package com.gc.baselib.utils;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * 分割器，footer，header自行处理
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private int left;
    private int bottom;
    private int middle;
    public DividerItemDecoration(int interval) {
        left=interval;
        bottom=interval;
        middle=interval;
    }
    public DividerItemDecoration(int left, int bottom, int middle) {
        this.left=left;
        this.bottom=bottom;
        this.middle=middle;
//        setIgnoreFooterDivider(true,1);
    }
    private boolean hasHeader;
    private int  headerCount;
    private boolean hasFooter;
    private int footerCount;

    public DividerItemDecoration setIgnoreHeaderDivider(boolean hasHeader,int headerCount){
        this.hasHeader=hasHeader;
        this.headerCount=headerCount;
        return this;
    }
    public DividerItemDecoration setIgnoreFooterDivider(boolean hasFooter,int footerCount){
        this.hasFooter=hasFooter;
        this.footerCount=footerCount;
        return this;
    }




    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        int size=parent.getAdapter().getItemCount();
        RecyclerView.LayoutManager layoutManager =parent.getLayoutManager();
        if(hasHeader&&position<headerCount){
            return;
        }
        if(hasFooter&&position>=size-footerCount){
            return;
        }
        if(layoutManager instanceof GridLayoutManager){
            GridLayoutManager gridLayoutManager= (GridLayoutManager) layoutManager;
            GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();
            int orientation=gridLayoutManager.getOrientation();
            // 获取item在span中的下标
            int spanIndex = params.getSpanIndex();
            int count=gridLayoutManager.getSpanCount();

//            if(GridLayoutManager.HORIZONTAL==orientation){
//
//            }else{
            if(position<=headerCount+count-1){//第一排
                outRect.top=bottom;
            }
            if(spanIndex%count==0){//最左边
                outRect.left=left;
                outRect.right=middle/2;
                outRect.bottom=bottom;
            }else if(spanIndex%count==count-1){//最右边
                outRect.right=left;
                outRect.left=middle/2;
                outRect.bottom=bottom;
            }else{//中间部分
                outRect.left=middle/2;
                outRect.right=middle/2;
                outRect.bottom=bottom;
            }


        }else if(layoutManager instanceof   LinearLayoutManager){
            LinearLayoutManager linearLayoutManager= (LinearLayoutManager) layoutManager;
            int orientation=linearLayoutManager.getOrientation();
            if(LinearLayoutManager.HORIZONTAL==orientation){
                if(hasHeader&&hasFooter){
                    outRect.left=middle/2;
                    outRect.right=middle/2;
                }else if(hasHeader&&!hasFooter){
                    if(position==size-1){
                        outRect.left=middle/2;
                        outRect.right=left;
                    }else{
                        outRect.left=middle/2;
                        outRect.right=middle/2;
                    }
                }else if(!hasHeader&&hasFooter){
                    if(position==0){
                        outRect.left=left;
                        outRect.right=middle/2;
                    }else{
                        outRect.left=middle/2;
                        outRect.right=middle/2;
                    }
                }
                outRect.bottom=bottom;
                outRect.top=bottom;
            }else{
                if(hasHeader&&hasFooter){
                    outRect.bottom=middle/2;
                    outRect.top=middle/2;
                }else if(hasHeader&&!hasFooter){
                    if(position==size-1){
                        outRect.top=middle/2;
                        outRect.bottom=bottom;
                    }else{
                        outRect.top=middle/2;
                        outRect.bottom=middle/2;
                    }
                }else if(!hasHeader&&hasFooter){
                    if(position==0){
                        outRect.top=bottom;
                        outRect.bottom=middle/2;
                    }else{
                        outRect.bottom=middle/2;
                        outRect.bottom=middle/2;
                    }
                }
                outRect.left=left;
                outRect.right=left;
            }

//            }



        }else if(layoutManager instanceof StaggeredGridLayoutManager){
            StaggeredGridLayoutManager gridLayoutManager= (StaggeredGridLayoutManager) layoutManager;
            StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
            // 获取item在span中的下标
            int spanIndex = params.getSpanIndex();
            int count=gridLayoutManager.getSpanCount();
            if(position<=headerCount+count-1){//第一排
                outRect.top=bottom;
            }
            if(spanIndex%count==0){//最左边
                outRect.left=left;
                outRect.right=middle/2;
                outRect.bottom=bottom;
            }else if(spanIndex%count==count-1){//最右边
                outRect.right=left;
                outRect.left=middle/2;
                outRect.bottom=bottom;
            }else{//中间部分
                outRect.left=middle/2;
                outRect.right=middle/2;
                outRect.bottom=bottom;
            }
        }


    }
}